package connect3.team7;

import java.rmi.RemoteException;

import connect3.team7.*;
import connect3.team7.util.*;

import java.util.*;
import java.io.*;

public class Connect3 {
  public static void main(String args[]) throws RemoteException, IOException {
    if (args[0].equals("server")) {
      HostConnectionSettings hcs = new HostConnectionSettings(); 
      hcs.handle       = "nbServer"; 
      hcs.ip           = IPUtil.getHostIP();
      hcs.port         = 1091;
      hcs.player_slots = 6;

      HostController h = HostController.getInstance(hcs);
    } else if (args[0].equals("client")) {
      Player client = new Player("nbClient");

      ClientConnectionSettings ccs = new ClientConnectionSettings();
      ccs.ip    = IPUtil.getHostIP();
      ccs.port  = 1091;

      ClientController c = ClientController.getInstance(ccs);
      
      ArrayList<HostConnectionSettings> lst = c.getAvailableHosts();
      Iterator<HostConnectionSettings> i = lst.iterator();


      int counter = 0;

      while(i.hasNext()) {
        HostConnectionSettings curr = i.next();

        System.out.println(counter + " - " + curr.handle + " - " + curr.ip + " - " + curr.port + " - " + curr.player_slots);
        counter++;
      } 

      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("$ ");
      int input =  (new Integer(stdin.readLine())).intValue();
      c.retrieveRemoteHost(lst.get(input));
      boolean b = c.join(client, ccs);

      if (b) {
        System.out.print("Enter x coordinate please: ");
        int x =  (new Integer(stdin.readLine())).intValue();
        System.out.print("Enter y coordinate please: ");
        int y =  (new Integer(stdin.readLine())).intValue();
        System.out.println("sending token to server ...");
        System.out.println(client.getName());
        Token t = new Token(x,y,client.getName());
        c.sendMoveToHost(t);
      } 
    }
  }
}
